package com.ruoyi.apis.service.impl;

import com.ruoyi.apis.entity.BannerEntity;
import com.ruoyi.apis.entity.OnlineEntity;
import com.ruoyi.apis.repository.BannerRepository;
import com.ruoyi.apis.repository.OnlineRepository;
import com.ruoyi.apis.service.BannerService;
import com.ruoyi.apis.service.OnlineService;
import com.ruoyi.apis.vo.OnlineVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Choi
 * @Date 2021/7/19
 */

@Service
public class OnlineServiceImpl implements OnlineService {

    @Autowired
    private OnlineRepository onlineRepository;

    @Override
    public OnlineVO getById(long onlineId) {
        OnlineEntity onlineEntity = onlineRepository.findById(onlineId);
        if (onlineEntity == null) {
            return null;
        }
        return new OnlineVO(onlineEntity);
    }

    @Override
    public Page<OnlineEntity> getOnlines(Integer page, Integer size) {
        Pageable pageable = PageRequest.of(page, size);
        return onlineRepository.findAllByOrderBySortAsc(pageable);
    }

    @Override
    public List<OnlineEntity> getByTitle(String title) {
        return onlineRepository.findAllByTitleLikeOrderBySortAsc(title);
    }
}
